package com.bittech.servlet.smallfileserver.web.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * Author: secondriver
 * Created: 2018/8/27
 */
public class FileApiBaseServlet extends HttpServlet {
    
    private Gson gson = new GsonBuilder()
            .disableHtmlEscaping()
            .create();
    
    void sendData(HttpServletResponse response, Object data, String message) {
        response.setContentType("application/javascript");
        HashMap<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("message", message);
        String content = gson.toJson(result);
        try {
            response.getWriter().write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
